package xcmg.device.web.announcement;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.announcement.BasicAnnouncementService;
import xcmg.device.service.vo.announcement.BasicAnnouncementVO;
import xcmg.device.service.vo.announcement.BasicAttachmentVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/basicAnnouncement")
public class BasicAnnouncementController extends BasicController {

    @Autowired
    private BasicAnnouncementService basicAnnouncementService;

    @Autowired
    private FastGenClient fastGenClient;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("通告信息查询")
    public ActionResult list(@RequestBody SearchCommonVO<BasicAnnouncementVO> condition)throws Exception{
        BasicAnnouncementVO basicAnnouncementVO = condition.getFilters();
        if(basicAnnouncementVO == null){
            basicAnnouncementVO = new BasicAnnouncementVO();
        }
        basicAnnouncementVO.setBeActivity(false);
        //根据条件查询当前企业下的所有通告信息
        basicAnnouncementVO.setOrgId(getOrgId());
        condition.setFilters(basicAnnouncementVO);
        PageCommonVO pageCommonVO = basicAnnouncementService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/listActivity",method = RequestMethod.GET)
//    @FastMappingInfo(needLogin = true)
    @ApiOperation("获取活动")
    public ActionResult listActivity()throws Exception{
        return actionValueResult(basicAnnouncementService.listActivity());
    }

    @RequestMapping(value = "/listByDealer",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("代理商通告信息查询")
    public ActionResult listByDealer(@RequestBody SearchCommonVO<BasicAnnouncementVO> condition)throws Exception{
        BasicAnnouncementVO basicAnnouncementVO = condition.getFilters();
        if(basicAnnouncementVO == null){
            //构造空的查询条件
            basicAnnouncementVO = new BasicAnnouncementVO();
        }
        basicAnnouncementVO.setOrgId(getOrgId());
        basicAnnouncementVO.setUserId(getUserId());
        condition.setFilters(basicAnnouncementVO);
        PageCommonVO pageCommonVO = basicAnnouncementService.listByDealer(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("通告信息添加")
    public ActionResult addAnnouncement(@RequestBody BasicAnnouncementVO basicAnnouncementVO)throws Exception{
        if(null == basicAnnouncementVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        String userId = getUserId();
        String userName = getUserName();
        ActionResult<String> textResult = fastGenClient.textGuid();
        String announceId = textResult.getValue();
        basicAnnouncementVO.setId(announceId);
        basicAnnouncementVO.setOrgId(getOrgId());
        basicAnnouncementVO.setCreateId(userId);
        basicAnnouncementVO.setCreateName(userName);
        basicAnnouncementVO.setCreateDate(new Date());
        basicAnnouncementVO.setUpdateId(userId);
        basicAnnouncementVO.setUpdateDate(new Date());
        List<BasicAttachmentVO> basicAttachmentVOList = basicAnnouncementVO.getBasicAttachmentList();
        List<BasicAttachmentVO> volist = new ArrayList<>();
        for (BasicAttachmentVO basicAttachmentVO : basicAttachmentVOList ) {
            basicAttachmentVO.setAnnouncementId(announceId);
            basicAttachmentVO.setCreateId(userId);
            basicAttachmentVO.setCreateName(userName);
            basicAttachmentVO.setCreateDate(new Date());
            basicAttachmentVO.setUpdateId(userId);
            basicAttachmentVO.setUpdateDate(new Date());
            volist.add(basicAttachmentVO);
        }
        basicAnnouncementVO.setBasicAttachmentList(volist);
        try {
            return actionResult(basicAnnouncementService.add(basicAnnouncementVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.AnnouncementManage.Announcement_EDIT)
    @ApiOperation(value = "更新通知公告信息")
    public ActionResult update(@RequestBody BasicAnnouncementVO model)throws Exception{
        String userId = getUserId();
        //更新参数是否存在
        if(model==null){
            return actionResult(ErrorCode.IllegalArument);
        }
        model.setUpdateId(userId);
        model.setOrgId(getOrgId());
        model.setUpdateDate(new Date());
        List<BasicAttachmentVO> basicAttachmentVOList = model.getBasicAttachmentList();
        basicAttachmentVOList.forEach(vo -> {
            vo.setCreateId(userId);
            vo.setCreateName(getUserName());
            vo.setCreateDate(new Date());
            vo.setUpdateId(getUserId());
            vo.setUpdateDate(new Date());
        });

        model.setBasicAttachmentList(basicAttachmentVOList);
        ErrorCode ret = basicAnnouncementService.update(model);

        return actionResult(ret);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "删除通知公告信息")
    public ActionResult remove(@RequestBody BasicAnnouncementVO model){
        String[] ids = model.getIds();
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret=basicAnnouncementService.remove(Arrays.asList(ids));
        return actionResult(ret);
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "通知公告信息单个查询")
    public ActionResult item(String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        BasicAnnouncementVO result=basicAnnouncementService.item(id);
        return actionValueResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/uploadAttach", method = RequestMethod.POST)
    @ApiOperation(value = "单个附件信息上传")
    @FastMappingInfo(needLogin = true,code = AuthCode.AnnouncementManage.Announcement_EDIT)
    public ActionResult uploadAttach(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile( multipartRequest.getFileNames().next());
        return actionValueResult(basicAnnouncementService.uploadAttach(file));
    }

    @RequestMapping(value = "/downloadAttach",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "单个附件信息下载")
    public void downloadAttach(@RequestParam("oldName") String oldName,@RequestParam("name")String name) throws Exception {
        basicAnnouncementService.downloadAttach(name,oldName);
    }

    @RequestMapping(value = "/miniPage",method = RequestMethod.POST)
    @ApiOperation("通知资讯--小程序")
    public ActionResult miniPage(@RequestBody SearchCommonVO<BasicAnnouncementVO> condition)throws Exception{
        BasicAnnouncementVO basicAnnouncementVO = condition.getFilters();
        if(basicAnnouncementVO == null){
            //构造空的查询条件
            basicAnnouncementVO = new BasicAnnouncementVO();
        }

        //全部游客可看
        basicAnnouncementVO.setCustomerId(null);
//        basicAnnouncementVO.setCustomerId(getCustomerId());
        condition.setFilters(basicAnnouncementVO);
        PageCommonVO pageCommonVO = basicAnnouncementService.miniPage(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    @ApiOperation(value = "通知咨询详情--小程序")
    public ActionResult getById(String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        BasicAnnouncementVO result = basicAnnouncementService.getById(id);
        return actionValueResult(result);
    }
}
